# 载入相关套件
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
import numpy as np

# 语料：最后一句为问题，其他为回答
corpus = [
    'This is the first document.',
    'This is the second document.',
    'And the third one.',
    'Is this the first document?',
]

# 将语料转换为词频矩阵，计算各个字词出现的次数。
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(corpus)

# 生字表
word = vectorizer.get_feature_names_out()
print ("Vocabulary：", word)

# 查看四句话的 BOW
print ("BOW=\n", X.toarray())


# TF-IDF 转换
transformer = TfidfTransformer()
tfidf = transformer.fit_transform(X)
print ("TF-IDF=\n", np.around(tfidf.toarray(), 4))

# 最后一句与其他句的相似度比较
from sklearn.metrics.pairwise import cosine_similarity
print (cosine_similarity(tfidf[-1], tfidf[:-1], dense_output=False))